//
//  VideoPlaybackViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/3/9.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class VideoPlaybackViewController: UIViewController {
    
    var urlStr : NSString?
    var scrollView : UIScrollView!
    var playView : CWPlayer!
    var describeLabel : UILabel!
    var describeLabelHeight = CFloat(0.0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initNavView()
        
        self.initView()
        
        self.requestPlayData(str: urlStr!)
    }
    
    func initNavView() {
        self.navigationController?.isNavigationBarHidden = false
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        self.navigationItem.title = "视频播放"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(backAction(sender:)))
    }
    
    func initView() {
        scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: self.navgationBarBottom, width: MAINSCREEN_WIDTH, height: MAINSCREEN_HEIGHT-self.navgationBarBottom));
        scrollView.backgroundColor = UIColor.defaultBackgroundColor
        scrollView.contentSize = CGSize.init(width: scrollView.width, height: scrollView.height+1)
        self.view.addSubview(scrollView)
        
        playView = CWPlayer.init(frame: CGRect.init(x: 0, y: 0, width: scrollView.width, height: 200))
        playView.contrainerVC = self
        playView.orSuperView = scrollView
        scrollView.addSubview(playView)
        
        let jjLabel = UILabel.init(frame: CGRect.init(x: 12, y: playView.bottom, width: scrollView.width-24, height: 50))
        jjLabel.font = UIFont.systemFont(ofSize: 16)
        jjLabel.textColor = UIColor.defaultTextColor
        jjLabel.text = "简介："
        scrollView.addSubview(jjLabel)
        
        describeLabel = UILabel.init(frame: CGRect.init(x: jjLabel.left, y: jjLabel.bottom, width: jjLabel.width, height: scrollView.height-jjLabel.bottom))
        describeLabel.backgroundColor = scrollView.backgroundColor
        describeLabel.font = UIFont.systemFont(ofSize: 15)
        describeLabel.textColor = UIColor.lightGray
        describeLabel.numberOfLines = 0
        scrollView.addSubview(describeLabel)
        
        describeLabelHeight = CFloat(describeLabel.height)
    }

    
    func requestPlayData(str: NSString) {
        self.view.makeToastActivity(.center)
        
        let url = "http://res.static.mojing.cn/171101-1-28-1/ios/zh/"+(str as String)
        Alamofire.request(url, method: .get).responseJSON {
            [weak self] (response) in
            self?.view.hideToastActivity()
            
            if let responseCC = response.result.value {
                let json = JSON(responseCC)
                let status = json["status"].string
                if status == "0" {
                    let play_url = json["data","albums",0,"videos",0,"play_url"].string // slice_path
                    
                    //playView.urlString(url: "http://120.25.226.186:32812/resources/videos/minion_02.mp4")
                    self?.playView.urlString(url: play_url! as NSString)
                    
                    let title = json["data","title"].string //名称
                    let cate = json["data","cate"].string  //类型
                    let actors = json["data","actors"].string //主演
                    let directors = json["data","directors"].string  //导演
                    let year = json["data","year"].string  //年代
                    let area = json["data","area"].string  //地区
                    let description = json["data","description"].string //描述
                    let describe = NSString.init(format: "名称：%@\n类型：%@\n主演：%@\n导演：%@\n年代：%@\n地区：%@\n介绍：%@", title!,cate!,actors!,directors!,year!,area!,description!)
                    
                    self?.addTextAction(describe: describe)
                }
            }
        }
    }
    
    func addTextAction(describe: NSString) {
        let paragraphStyle = NSMutableParagraphStyle.init()
        paragraphStyle.lineSpacing = 10
        let attrDic = [NSAttributedStringKey.paragraphStyle : paragraphStyle , NSAttributedStringKey.font : describeLabel.font] as [NSAttributedStringKey : Any]
        let attr = NSMutableAttributedString.init(string: describe as String, attributes: attrDic)
        self.describeLabel.attributedText = attr
        
        //calculate height
        let size = SizeWithFont(content: attr.string as NSString, attrDic: attrDic as NSDictionary, maxSize: CGSize.init(width: (self.describeLabel.width), height: 1000))
        self.describeLabel.height = size.height
        if CGFloat(size.height) > CGFloat(describeLabelHeight) {
            scrollView.contentSize = CGSize.init(width: scrollView.width, height: scrollView.height+1+(size.height-CGFloat(describeLabelHeight)))
        }
    }
    
    @objc func backAction(sender: UIBarButtonItem) {
        playView.playerDealloc()
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


